package cn.itwangxiang.android.ui;

import cn.itwangxiang.android.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DemoHandler extends Activity {
	private TextView textView;
	private Button button;

	private Handler demoHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what >= 0) {
				Toast.makeText(DemoHandler.this, "成功获取Message.obj中的内容！",
						Toast.LENGTH_SHORT).show();
				textView.setText(msg.obj.toString());
			} else {
				Toast.makeText(DemoHandler.this, "获取Message失败！", Toast.LENGTH_SHORT);
			}
		};
	};

	private Runnable runnable = new Thread() {
		public void run() {
			Message msg = new Message();
			msg.obj = "这是要通过Message.obj发送的内容";
			demoHandler.sendMessage(msg);
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.demo_handler);
		textView = (TextView) findViewById(R.id.textView1);
		button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				
				new Thread(runnable).start();
			}
		});

	}

}
